package ar.edu.itba.it.paw.web;

import java.util.List;

import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.validation.AbstractFormValidator;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import ar.edu.itba.it.paw.domain.OrderProducts;
import ar.edu.itba.it.paw.domain.Product;
import ar.edu.itba.it.paw.repo.ProductRepo;
import ar.edu.itba.it.paw.repo.UserRepo;
import ar.edu.itba.it.paw.web.base.BasePage;
import ar.edu.itba.it.paw.web.component.TypeLabel;
import ar.edu.itba.it.paw.web.product.ProductDetailPage;
import ar.edu.itba.it.paw.web.user.RestorePasswordPage;
import ar.edu.itba.it.paw.web.user.UserMenuPage;



public class HomePage extends BasePage {
	
	
	@SpringBean
	private ProductRepo productRepo;
	@SpringBean
	private UserRepo userRepo;
	
	private TextField<String> username_field;
	private TextField<String> password_field;
	
	private transient String username;
	private transient String password;
	
	public HomePage() {
		
		if(MercadoLibreSession.get().getUser() != null){
			setResponsePage(UserMenuPage.class);
			return;
		}
		
		add(new FeedbackPanel("feedback").add(new SimpleAttributeModifier("style", "color: #FF0000")));
		Form<HomePage> form = new Form<HomePage>("loginForm", new CompoundPropertyModel<HomePage>(this)) {
			@Override
			protected void onSubmit() {
				MercadoLibreSession session = MercadoLibreSession.get();
				if(session.signIn(username, password)){
					if (!continueToOriginalDestination()) {
						setResponsePage(UserMenuPage.class);
					}
					setRedirect(true);
				}
			}
		};

		username_field = new TextField<String>("username", new PropertyModel<String>(this,"username"));
		form.add(username_field.setRequired(true));
		password_field = new PasswordTextField("password", new PropertyModel<String>(this,"password"));
		form.add(password_field.setRequired(true));
		form.add(new Button("login", new ResourceModel("login")));
		form.add(new AbstractFormValidator(){

			@Override
			public FormComponent<?>[] getDependentFormComponents() {
				return new FormComponent[] {username_field,password_field};

			}

			@Override
			public void validate(Form<?> form) {
				if(!userRepo.authenticate(username_field.getConvertedInput() ,password_field.getConvertedInput())){
					form.error(getString("invalidCredentials"));
					return;
				}
				
			}
			
		});
		add(form);
		add(new Link<RestorePasswordPage>("restorePass", new CompoundPropertyModel<RestorePasswordPage>(this)) {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				setResponsePage(RestorePasswordPage.class);
			}
		});
		IModel<List<Product>> lastProductsModel = new LoadableDetachableModel<List<Product>>() {
			@Override
			protected List<Product> load() {
				return productRepo.getAllProducts(new OrderProducts("release_date",false,5)); 
			}
		};
		IModel<List<Product>> visitedProductsModel = new LoadableDetachableModel<List<Product>>() {
			@Override
			protected List<Product> load() {
				return productRepo.getVisitedProducts(5); 
			}
		};
		
		
		add(new PropertyListView<Product>("lastProducts", lastProductsModel) {
			
			@Override
			protected void populateItem(ListItem<Product> item) {
				item.add(new Label("d_name",new PropertyModel<String>(item.getModel(), "name")).add(new SimpleAttributeModifier("style", "color: #FF00FF")));
				item.add(new Label("release_date").add(new SimpleAttributeModifier("style", "")));
				item.add(new TypeLabel("d_type",item.getModel()));
				item.add(new Link<Product>("d_detail", item.getModel()) {
					@Override
					public void onClick() {
						setResponsePage(new ProductDetailPage(getModelObject()));
					}
				}.add(new Image("d_detail_image", MercadoLibre.DETAIL_ICON).add(new SimpleAttributeModifier("height","15px"))));
			}
		});
		add(new PropertyListView<Product>("visitedProducts", visitedProductsModel) {
			
			@Override
			protected void populateItem(ListItem<Product> item) {
				item.add(new Label("v_name",new PropertyModel<String>(item.getModel(), "name")).add(new SimpleAttributeModifier("style", "color: #FF00FF")));
				item.add(new Label("visits").add(new SimpleAttributeModifier("style", "")));
				item.add(new TypeLabel("v_type", item.getModel()));
				item.add(new Link<Product>("v_detail", item.getModel()) {
					@Override
					public void onClick() {
						System.out.println("mando a la pag de mos prod");
						setResponsePage(new ProductDetailPage(getModelObject()));
					}
				}.add(new Image("v_detail_image", MercadoLibre.DETAIL_ICON).add(new SimpleAttributeModifier("height","15px"))));
			}
		});
	}
	
}
